package cn.iocoder.yudao.module.ems.controller.admin.classification;

import cn.iocoder.yudao.framework.common.enums.CommonStatusEnum;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.annotations.*;

import javax.validation.*;
import java.util.*;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.module.ems.controller.admin.classification.vo.*;
import cn.iocoder.yudao.module.ems.dal.dataobject.classification.ClassificationDO;
import cn.iocoder.yudao.module.ems.convert.classification.ClassificationConvert;
import cn.iocoder.yudao.module.ems.service.classification.ClassificationService;

@Api(tags = "管理后台 - 分类")
@RestController
@RequestMapping("/ems/classification")
@Validated
public class ClassificationController {

    @Resource
    private ClassificationService classificationService;

    @PostMapping("/create")
    @ApiOperation("创建分类")
    @PreAuthorize("@ss.hasPermission('ems:classification:create')")
    public CommonResult<Long> createClassification(@Valid @RequestBody ClassificationCreateReqVO createReqVO) {
        return success(classificationService.createClassification(createReqVO));
    }

    @PutMapping("/update")
    @ApiOperation("更新分类")
    @PreAuthorize("@ss.hasPermission('ems:classification:update')")
    public CommonResult<Boolean> updateClassification(@Valid @RequestBody ClassificationUpdateReqVO updateReqVO) {
        classificationService.updateClassification(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @ApiOperation("删除分类")
    @ApiImplicitParam(name = "id", value = "编号", required = true, dataTypeClass = Long.class)
    @PreAuthorize("@ss.hasPermission('ems:classification:delete')")
    public CommonResult<Boolean> deleteClassification(@RequestParam("id") Long id) {
        classificationService.deleteClassification(id);
        return success(true);
    }

    @GetMapping("/get")
    @ApiOperation("获得分类")
    @ApiImplicitParam(name = "id", value = "编号", required = true, example = "1024", dataTypeClass = Long.class)
    @PreAuthorize("@ss.hasPermission('ems:classification:query')")
    public CommonResult<ClassificationRespVO> getClassification(@RequestParam("id") Long id) {
        ClassificationDO classification = classificationService.getClassification(id);
        return success(ClassificationConvert.INSTANCE.convert(classification));
    }

//    @GetMapping("/list")
//    @ApiOperation("获得分类列表")
//    @ApiImplicitParam(name = "ids", value = "编号列表", required = true, example = "1024,2048", dataTypeClass = List.class)
//    @PreAuthorize("@ss.hasPermission('ems:classification:query')")
//    public CommonResult<List<ClassificationRespVO>> getClassificationList(@RequestParam("ids") Collection<Long> ids) {
//        List<ClassificationDO> list = classificationService.getClassificationList(ids);
//        return success(ClassificationConvert.INSTANCE.convertList(list));
//    }

    @GetMapping("/list")
    @ApiOperation("获取分类列表")
    @PreAuthorize("@ss.hasPermission('ems:classification:query')")
    public CommonResult<List<ClassificationRespVO>> listClassifications(ClassificationListReqVO reqVO) {
        List<ClassificationDO> list = classificationService.getSimpleClassifications(reqVO);
        list.sort(Comparator.comparing(ClassificationDO::getSort));
        return success(ClassificationConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/list-all-simple")
    @ApiOperation(value = "获取分类精简信息列表", notes = "只包含被开启的分类，主要用于前端的下拉选项")
    public CommonResult<List<ClassificationSimpleRespVO>> getSimpleClassifications() {
        // 获得分类列表，只要开启状态的
        ClassificationListReqVO reqVO = new ClassificationListReqVO();
        reqVO.setStatus(CommonStatusEnum.ENABLE.getStatus());
        List<ClassificationDO> list = classificationService.getSimpleClassifications(reqVO);
        // 排序后，返回给前端
        list.sort(Comparator.comparing(ClassificationDO::getSort));
        return success(ClassificationConvert.INSTANCE.convertList02(list));
    }

    @GetMapping("/page")
    @ApiOperation("获得分类分页")
    @PreAuthorize("@ss.hasPermission('ems:classification:query')")
    public CommonResult<PageResult<ClassificationRespVO>> getClassificationPage(@Valid ClassificationPageReqVO pageVO) {
        PageResult<ClassificationDO> pageResult = classificationService.getClassificationPage(pageVO);
        return success(ClassificationConvert.INSTANCE.convertPage(pageResult));
    }

//    @GetMapping("/export-excel")
//    @ApiOperation("导出分类 Excel")
//    @PreAuthorize("@ss.hasPermission('ems:classification:export')")
//    @OperateLog(type = EXPORT)
//    public void exportClassificationExcel(@Valid ClassificationExportReqVO exportReqVO,
//              HttpServletResponse response) throws IOException {
//        List<ClassificationDO> list = classificationService.getClassificationList(exportReqVO);
//        // 导出 Excel
//        List<ClassificationExcelVO> datas = ClassificationConvert.INSTANCE.convertList02(list);
//        ExcelUtils.write(response, "分类.xls", "数据", ClassificationExcelVO.class, datas);
//    }

}
